import Vue from 'vue';
import Vuex from 'vuex';
import { getToken } from '../utils/auth.js';
Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
        //用户信息
        member: {},
        // 加载状态
        mainLoading: false,
        lawData :{},
        loginState:false,
        regState:false,
        forgotState:false,
        token:getToken()
    },
    mutations: {
        //更新用户信息
        SET_USER_INFO(state, userinfo) {
            state.member = userinfo;
        },
        // loading的显示
        mainLoading(state, show) {
            state.mainLoading = show;
        },
        SET_LAW:(state,list)=>{
            state.lawData = list;
        },
        SET_LOGIN_STATE:(state,status)=>{
            state.loginState = status;
        },
        SET_REG_STATE:(state,status)=>{
            state.regState = status;
        },
        SET_FORGOT_STATE:(state,status)=>{
            state.forgotState = status;
        },
        SET_TOKEN:(state,token)=>{
            state.token = token;
        }
    },
    getters:{
        member:state=>state.member,
        categoryList:state=>state.lawData,
    },
    actions: {
        setUserInfo({ commit, state }, userinfo) {
            return new Promise(resolve => {
                commit('SET_USER_INFO', userinfo);
                resolve();
            });
        },
        setLawList({ commit, state },data){
            commit('SET_LAW',data)
        },
        login:({commit},status)=>{
            commit('SET_LOGIN_STATE',status);
        },
        changeRegState:({commit},status)=>{
            commit('SET_REG_STATE',status);
        },
        forgot:({commit},status)=>{
            commit('SET_FORGOT_STATE',status);
        },
        setToken:({commit},token)=>{
            return new Promise(resolve => {
                commit('SET_TOKEN', token);
                resolve();
            });
        }
    },
});

export default store;
